/**
 * 
 */
package com.wsw.ssh.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import com.wsw.common.action.BaseAction;
import com.wsw.common.model.Tree;
import com.wsw.common.util.BeanUtils;
import com.wsw.common.util.HqlFilter;
import com.wsw.ssh.system.model.Menu;
import com.wsw.ssh.system.service.IMenuService;

/**
 * 
 * File：MenuAction.java 
 * title：
 * Description:
 * Copyright: Copyright (c) 2014
 *
 * @author sw.wang
 * @version 
 */
@Namespace("/base")
@Action
public class MenuAction extends BaseAction<Menu>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource
	public void setService(IMenuService service) {
		this.service = service;
	}
	
	/**
	 * 获得主菜单tree，也用于获得上级资源菜单combotree
	 */
	public void doNotNeedSecurity_getMainMenu() {
		HqlFilter hqlFilter = new HqlFilter(getRequest());
		/*SessionInfo sessionInfo = (SessionInfo) getSession().getAttribute(ConfigUtil.getSessionInfoName());
		hqlFilter.addFilter("QUERY_user#id_S_EQ", sessionInfo.geuser().getId());
		hqlFilter.addFilter("QUERY_t#syresourcetype#id_S_EQ", "0");// 0就是只查菜单*/
		List<Menu> menus =  ((IMenuService)service).getMainMenu(hqlFilter);
		List<Tree> tree = new ArrayList<Tree>();
		for (Menu menu : menus) {
			Tree node = new Tree();
			try {
				BeanUtils.copyNotNullProperties(menu, node);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			node.setText(menu.getName());
			Map<String, String> attributes = new HashMap<String, String>();
			attributes.put("url", menu.getUrl());
			node.setAttributes(attributes);
			tree.add(node);
		}
		writeJson(tree);
	}
	
	/**
	 * 获得资源treeGrid
	 */
	public void treeGrid() {
		HqlFilter hqlFilter = new HqlFilter(getRequest());
		//SessionInfo sessionInfo = (SessionInfo) getSession().getAttribute(ConfigUtil.getSessionInfoName());
		//hqlFilter.addFilter("QUERY_user#id_S_EQ", sessionInfo.geuser().getId());
		writeJson(((IMenuService) service).findByFilter(hqlFilter));
	}
}
